package org.jcms.web.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jcms.model.Manager;
import org.jcms.model.Order;
import org.jcms.model.State;
import org.jcms.service.OrderServiceI;
import org.jcms.service.StateServiceI;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 * 审核订单，参数：type:0表示审核通过 ，其它参数 orderId 审核订单，参数：type:1表示审核未通过 ，其它参数 orderId
 * 
 * @author Administrator
 * 
 */
public class ManagerVerifyOrder extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public ManagerVerifyOrder() {
		super();
	}

	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		ApplicationContext ac = WebApplicationContextUtils
				.getRequiredWebApplicationContext(this.getServletContext());
		OrderServiceI orderServiceI = (OrderServiceI) ac
				.getBean("orderServiceImpl");
		StateServiceI stateServiceI = (StateServiceI) ac
				.getBean("stateServiceImpl");
		Manager manager = (Manager) request.getSession().getAttribute(
				"manager-0");
		Manager manager2 = (Manager) request.getSession().getAttribute(
				"manager-1");
		String retJson = "{\"error\":@err,\"msg\":\"@msg\"}";
		String err = "1";
		String msg = "";
		String orderId = request.getParameter("orderId");
		String type = request.getParameter("type");

		try {

			if (manager == null && manager2 == null) {
				msg = "网页已过期";
				return;
			}
			if (manager == null) {
				msg = "只有教材发行人员才有该操作权限";
				return;
			}
			if (orderId == null || "".equals(orderId)) {
				msg = "订单ID不能为空";
				return;
			}
			if (type == null || "".equals(type)) {
				msg = "type不能为空";
				return;
			}

			int intType = Integer.parseInt(type);
			Order order = orderServiceI.findById(orderId);
			if (order == null) {
				msg = "要审核的订单不存在";
				return;
			}

			if (order.getState().getStateId() != 0) {
				if (order.getState().getStateId() <= 4)
					msg = "该订单已经审核过，目前状态为：" + order.getState().getStateName();
				else
					msg = "该订单已取消，不能审核";
				return;
			}
			State	state =null;
			switch (intType) {
			case 0:
				state = stateServiceI.findById(1);
				msg = "订单(" + orderId + ")通过审核";
				break;
			case 1:
				 state = stateServiceI.findById(4);
				msg = "订单(" + orderId + ")审核未通过";
				break;

			default:
				msg = "type参数不正确";
				return;
			}
			order.setState(state);
			orderServiceI.update(order);
			err = "0";

		} catch (Exception e) {
			msg = "系统出错";
		} finally {
			retJson = retJson.replace("@err", err);
			retJson = retJson.replace("@msg", msg);
			response.getOutputStream().write(retJson.getBytes("utf-8"));
		}
	}

	public void init() throws ServletException {

	}

}
